package com.ymh.git.thread.juc;

import java.util.concurrent.ExecutionException;

/**
 * Created by aivin on 2018/10/28.
 */
public class HasInterruptException {

    private static class UserThread extends Thread {

        public UserThread(String threadname) {
            super(threadname);
        }

        @Override
        public void run() {
            while (!isInterrupted()) { //判断线程是否中断
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    System.out.println(Thread.currentThread().getName() + " interrupt flag is: " + isInterrupted());
                    interrupt();
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "extends Thread");
            }
            System.out.println(Thread.currentThread().getName() + " interrupt flag is: " + isInterrupted());
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Thread myThread = new UserThread("HasInterruptExceptionThread");
//        myThread.setDaemon(true);//设置守护线程
        myThread.start();
        Thread.sleep(200);
        myThread.interrupt();// 中断线程
//        myThread.setPriority();//设置线程优先级，不同的操作系统之间存在很大差异，甚至有些操作系统会忽略该设置
    }


}
